﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace Region
{
    [DBObject(Table = "plan", PrimaryKeyPole = "PlanID")]
    public class Plan : DBObject
    {
        [DBColumn]
        public Int32 PlanID { get; set; }
      /// <summary>номер запланированной проверки</summary>
        [DBColumn]
        public Int32 PlanNum { get; set; }
        [DBColumn]
        public Int32 KontID { get; set; }
        [DBColumn]
        public Int32 PlanYear { get; set; }
        [DBColumn]
        public Int32 RegNum { get; set; }
        [DBColumn(PoleType = EPoleType.RegionDate)]
        public RegionDate Start { get; set; }
        [DBColumn(PoleType = EPoleType.RegionDate)]
        public RegionDate End { get; set; }
        [DBColumn]
        public Int32 TimeDay { get; set; }
        [DBColumn]
        public Int32 TimeH { get; set; }
      public override bool read(MySqlConnection RegionDB=null)
      {
          if (!base.read(RegionDB)) return false;
          return true;
      }
      public Plan(Engine engine, Int32 PlanID)
          : base(engine, PlanID)
      {
      }
  }
  public struct PlanListLine
  {
    public Int32 PlanID;
  }
  public class PlanList: DBList<PlanListLine>
  {
    public PlanList(Engine engine):base(engine)
    {
      DefaultPoles.Add("plan.PlanID");
      DefaultTables.Add("plan");
      DefaultOrderBy.Add("plan.PlanNum DESC");
    }
    protected override void ProceessWheres(SQLParam Params,List<String> Tables, List<String> Poles,List<String> Wheres,List<String> OrderBy)
    {
      Int32 PlanID, ParentOtdelID, PlanYear, LineInPage, Page;
        String[] DatePoles = { "Start", "End" };
        for (Int32 i = 0; i < DatePoles.Length; i++)
        {
            String Pole = DatePoles[i];
            if (m_wheres.ContainsKey(Pole + "Start"))
            { 
              RegionDate date = m_wheres[Pole + "Start"] as RegionDate;
              if (date!=null && !(date.Empty))
              {
                Wheres.Add("raspor." + Pole + " >= @" + Pole + "Start");
                Params["@" + Pole + "Start"] = date.getRegionDate();
              }
            }
            if (m_wheres.ContainsKey(Pole + "End"))
            {
              RegionDate date = m_wheres[Pole + "End"] as RegionDate;
              if (date!=null && !(date.Empty))
              {
                Wheres.Add("raspor." + Pole + " < @" + Pole + "End");
                Params["@" + Pole + "End"] = date.getRegionDate();
              }
            }
        }
        if (m_wheres.ContainsKey("PlanID") && m_wheres["PlanID"] != null && Int32.TryParse(m_wheres["PlanID"].ToString(), out PlanID) && PlanID>0)
        {
            Wheres.Add("plan.PlanID=@PlanID");
            Params["@PlanID"] = PlanID;
        }
        if (m_wheres.ContainsKey("ParentOtdelID") && m_wheres["ParentOtdelID"] != null && Int32.TryParse(m_wheres["ParentOtdelID"].ToString(), out ParentOtdelID))
        {
            Tables.Add("INNER JOIN otdel_otdel ON otdel_otdel.OtdelID=plan.OtdelID");
            Wheres.Add("otdel_otdel.ParentOtdelID=@ParentOtdelID");
            Params["@ParentOtdelID"] = ParentOtdelID;
        }
        if (m_wheres.ContainsKey("PlanYear") && m_wheres["PlanYear"] != null && Int32.TryParse(m_wheres["PlanYear"].ToString(), out PlanYear) && PlanYear>0)
        {
            Wheres.Add("plan.PlanYear=@PlanYear");
            Params["@PlanYear"] = PlanYear;
        }    
    }
    protected override PlanListLine readLine(Dictionary<String, Object> r)
    {
      var l = new PlanListLine();
      l.PlanID = (Int32)r["PlanID"];
      return l;
    }    
  }
  public partial class Engine
  {
      /// <summary> Список открытых распояжений </summary>
      private Dictionary<Int32, Plan> l_Plan;
      /// <summary> Получить распоряжение по его идентификатору</summary><param name="RasporID">ИД Распоряжения</param><returns>Объект распоряжения</returns>
      public Plan getPlan(Int32 PlanID)
      {
          if (l_Plan == null) l_Plan = new Dictionary<Int32, Plan>();
          if (l_Plan.ContainsKey(PlanID)) return (Plan)l_Plan[PlanID];
          Plan plan = new Plan(this, PlanID);
          l_Plan[PlanID] = plan;
          return plan;
      }
  }
}
